package com.rc.saas.mini.service.ugs;

import com.rc.saas.mini.mapper.ugs.TenantUgsSuperLevelPriceMapper;
import com.rc.saas.mini.mapper.ugs.UgsMemberSuperLevelMapper;
import com.rc.saas.mini.mapper.ugs.UgsMemberSuperLevelOrderMapper;
import com.rc.saas.mini.model.ugs.*;
import com.rc.saas.mini.vo.ugs.UgsMemberSuperLevelOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UgsMemberSuperLevelOrderServiceImpl implements UgsMemberSuperLevelOrderService {

    @Autowired
    private UgsMemberSuperLevelOrderMapper ugsMemberSuperLevelOrderMapper;

    @Autowired
    private UgsMemberSuperLevelMapper ugsMemberSuperLevelMapper;

    @Autowired
    private TenantUgsSuperLevelPriceMapper tenantUgsSuperLevelPriceMapper;

    @Override
    public UgsMemberSuperLevelOrderMapper getMapper() {
        return ugsMemberSuperLevelOrderMapper;
    }

    @Override
    public UgsMemberSuperLevelOrderVo selectBySuperOrderNum(String outTradeNo) {
        return ugsMemberSuperLevelOrderMapper.selectBySuperOrderNum(outTradeNo);
    }

    @Override
    public void updateSuperLevelOrder(Long superLevelOrderId, String paymentType, String transaction_id) {
        UgsMemberSuperLevelOrder ugsMemberSuperLevelOrder = ugsMemberSuperLevelOrderMapper.selectByPrimaryKey(superLevelOrderId);
        ugsMemberSuperLevelOrder.setPaymentType(paymentType);
        ugsMemberSuperLevelOrder.setTransactionId(transaction_id);
        ugsMemberSuperLevelOrder.setStatus("1");
        ugsMemberSuperLevelOrder.setLastUpdatedTime(new Date());
        UgsMemberSuperLevelExample ugsMemberSuperLevelExample = new UgsMemberSuperLevelExample();
        ugsMemberSuperLevelExample.createCriteria().andTenantCodeEqualTo(ugsMemberSuperLevelOrder.getTenantCode()).andMemberIdEqualTo(ugsMemberSuperLevelOrder.getMemberId());
        List<UgsMemberSuperLevel> ugsMemberSuperLevels = ugsMemberSuperLevelMapper.selectByExample(ugsMemberSuperLevelExample);
        TenantUgsSuperLevelPriceExample tenantUgsSuperLevelPriceExample = new TenantUgsSuperLevelPriceExample();
        tenantUgsSuperLevelPriceExample.createCriteria().andTenantCodeEqualTo(ugsMemberSuperLevelOrder.getTenantCode()).andSuperCardTypeEqualTo(ugsMemberSuperLevelOrder.getSuperCardType());
        List <TenantUgsSuperLevelPrice> tenantUgsSuperLevelPrices = tenantUgsSuperLevelPriceMapper.selectByExample(tenantUgsSuperLevelPriceExample);

        if (ugsMemberSuperLevels != null && ugsMemberSuperLevels.size() > 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            //todo 添加时间
            if (ugsMemberSuperLevels.get(0).getEndTime().compareTo(new Date()) == 1) {
                calendar.setTime(ugsMemberSuperLevels.get(0).getEndTime());
            }
            ugsMemberSuperLevelOrder.setBeginTime(ugsMemberSuperLevels.get(0).getEndTime());
            calendar.add(Calendar.DATE, tenantUgsSuperLevelPrices.get(0).getCardExpireDay());
            ugsMemberSuperLevelOrder.setEndTime(calendar.getTime());
        }else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            ugsMemberSuperLevelOrder.setBeginTime(new Date());
            calendar.add(Calendar.DATE, tenantUgsSuperLevelPrices.get(0).getCardExpireDay());
            ugsMemberSuperLevelOrder.setEndTime(calendar.getTime());
        }
    }
}
